<template>
   <a-drawer
    title="发送邮件"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="mailSendVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='发送人'
                   v-bind="formItemLayout">
        <a-input v-model="user.username"
                 @blur="handleUserNameBlur"
                 v-decorator="['username',{rules: [
            { type: 'email', message: '请输入正确的邮箱' },
            { max: 50, message: '长度不能超过50个字符'}
          ]}]"/>
      </a-form-item>
      <a-form-item label='主题' v-bind="formItemLayout">
        <a-input
          v-model="user.subject"
          v-decorator="['subject ',{rules: [
            { max: 50, message: '长度不能超过50个字符'}
          ]}]"/>
      </a-form-item>
    <vue-editor v-model="content" useCustomImageHandler   @imageAdded="handleImageAdded"></vue-editor>
      <div class="drawer-bootom-button">
        <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
          <a-button style="margin-right: .8rem">取消</a-button>
        </a-popconfirm>
        <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
      </div>
    </a-form>
  </a-drawer>

</template>

<script>
import { VueEditor } from 'vue2-editor'
const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'mailSend',
  components: { VueEditor },
  props: {
    mailSendVisible: {
      default: false
    }
  },
  data () {
    return {
      user: {
        username: ''
      },
      loading: false,
      roleData: [],
      formItemLayout,
      form: this.$form.createForm(this),
      validateStatus: '',
      help: '',
      content: 'fuck'
    }
  },
  methods: {
    handleImageAdded: function (file, Editor, cursorLocation, resetUploader) {
      // An example of using FormData
      // NOTE: Your key could be different such as:
      // formData.append('file', file)

      var formData = new FormData()
      formData.append('file', file)
      this.$upload('test/import', formData).then(result => {
        // Get url from response
        let url = result.data.url
        Editor.insertEmbed(cursorLocation, 'image', url)
        resetUploader()
      }).catch(() => {
        this.$message.error('上传图片失败!')
      })
    },
    reset () {
      this.validateStatus = ''
      this.help = ''
      this.user.username = ''
      this.loading = false
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleSubmit () {
      if (this.validateStatus !== 'success') {
        this.handleUserNameBlur()
      }
      this.form.validateFields((err, values) => {
        if (!err && this.validateStatus === 'success') {
          this.loading = true
          this.$post('user', {
            ...this.user
          }).then((r) => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    handleUserNameBlur () {
      let username = this.user.username.trim()
      if (username.length) {
        if (username.length > 10) {
          this.validateStatus = 'error'
          this.help = '用户名不能超过10个字符'
        }
      } else {
        this.validateStatus = 'error'
        this.help = '用户名不能为空'
      }
    }
  }
}
</script>
<style lang="less" scoped>
@import "../../../static/less/Common";
</style>
